package com.first.nell.components.input

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.base.BaseFormAction
import com.first.nell.databinding.ViewFirstNellLoginOtpCutdownBinding
import com.first.nell.extend.addFirstNellTipsFirstNellStyle
import com.first.nell.extend.addFirstNellUnderFirstNellLineFirstNellStyle
import com.first.nell.extend.recordFirstNellTagFirstNellEvent
import com.first.nell.extend.toFirstNell00FirstNellFormat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*

class LoginOtpCutdown : RelativeLayout, BaseFormAction {

    lateinit var mFirstNellViewFirstNellBinding: ViewFirstNellLoginOtpCutdownBinding
    private lateinit var vFirstNellRootFirstNellView: View

    private var mFirstNellSupportFirstNellFragmentFirstNellManager: FragmentManager? = null
    private var mFirstNellCutdownFirstNellFlow: Job? = null

    private var mFirstNellOnFirstNellActionFirstNellClickFirstNellListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            vFirstNellRootFirstNellView =
                View.inflate(it, R.layout.view_first_nell_login_otp_cutdown, this)
            mFirstNellViewFirstNellBinding =
                ViewFirstNellLoginOtpCutdownBinding.bind(vFirstNellRootFirstNellView)
            initFirstNellSubviews()
        }
    }

    override fun initFirstNellSubviews() {
        try {
            mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdownResend.setOnClickListener {
                mFirstNellOnFirstNellActionFirstNellClickFirstNellListener.invoke(0)
                //记录打点
                mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdownResend.recordFirstNellTagFirstNellEvent()
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    private fun initFirstNellCutdownFirstNellFlow(
        scope: CoroutineScope,
        onTick: (Int) -> Unit,
        onFinish: () -> Unit,
    ): Job {
        return flow {
            for (tickSec in 60 downTo 0) {
                emit(tickSec)
                if (tickSec != 0) {
                    delay(1000)
                }
            }
        }.flowOn(Dispatchers.Main)
            .onEach { onTick.invoke(it) }
            .onCompletion { onFinish.invoke() }
            .launchIn(scope)
    }

    @SuppressLint("SetTextI18n")
    fun startFirstNellCutdown(scope: CoroutineScope) {
        mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdown.visibility = VISIBLE
        mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdownResend.visibility = GONE
        mFirstNellCutdownFirstNellFlow = initFirstNellCutdownFirstNellFlow(scope,
            {
                if (it == 0) {
                    mFirstNellCutdownFirstNellFlow?.cancel()
                    mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdown.visibility = GONE
                    mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdownResend.visibility =
                        VISIBLE
                } else {
                    val secText = it.toFirstNell00FirstNellFormat()
                    mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdown.text =
                        "${context.getString(R.string.first_nell_code_expires_in_00)}${secText}"
                    mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdown.setTextColor(
                        ContextCompat.getColor(
                            context,
                            R.color.black_6002
                        )
                    )
                    mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdown.addFirstNellTipsFirstNellStyle(
                        secText,
                        1F,
                        ContextCompat.getColor(
                            context,
                            R.color.main_blue
                        )
                    )
                    mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdown.addFirstNellUnderFirstNellLineFirstNellStyle(
                        secText
                    )
                }
            },
            {
                mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdown.visibility = GONE
                mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdownResend.visibility = VISIBLE

                mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdownResend.text =
                    context.getString(R.string.first_nell_dont_receive_code_resend_code)
                mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdownResend.setTextColor(
                    ContextCompat.getColor(
                        context,
                        R.color.black_6002
                    )
                )
                mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdownResend.addFirstNellTipsFirstNellStyle(
                    context.getString(R.string.first_nell_resend_code),
                    1F,
                    ContextCompat.getColor(
                        context,
                        R.color.main_blue
                    )
                )
                mFirstNellViewFirstNellBinding.textLoginInputOtpOtpCutdownResend.addFirstNellUnderFirstNellLineFirstNellStyle(
                    context.getString(R.string.first_nell_resend_code)
                )
            })
    }

    fun stopFirstNellCutdown() {
        mFirstNellCutdownFirstNellFlow?.cancel()
    }

    override fun setFirstNellFragmentFirstNellManager(manager: FragmentManager) {
        mFirstNellSupportFirstNellFragmentFirstNellManager = manager
    }

    override fun setFirstNellOnFirstNellActionFirstNellClickFirstNellListener(listener: (Int) -> Unit) {
        mFirstNellOnFirstNellActionFirstNellClickFirstNellListener = listener
    }

    override fun setFirstNellTitle(title: String) {

    }

    override fun setFirstNellDetail(detail: String) {

    }

    override fun setFirstNellContent(content: String) {

    }

    override fun setFirstNellHint(hint: String) {

    }

    override fun setFirstNellDetailFirstNellColor(detailColor: Int) {

    }

    override fun setFirstNellTitleFirstNellColor(titleColor: Int) {

    }
}